package com.gy.spring.mvc.common.tools;

import java.io.Serializable;
import java.util.HashMap;

import com.gy.spring.mvc.common.interceptor.MyException;

/**
 * TODO 全局JSON统一
 *
 * @author geYang
 * @date 2018-04-10
 */
public class R extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;

    private final static int CODE_SUCCESS = 200;
    private final static int CODE_ERROR = 500;

    public R() {
    }

    public R(boolean b) {
        if (b) {
            put("code", CODE_SUCCESS);
        } else {
            put("code", CODE_ERROR);
        }
        put("success", b);
    }

    public static R ok() {
        return new R(true);
    }

    public static R ok(Object data) {
        R r = new R(true);
        r.put("data", data);
        return r;
    }

    public static R err() {
        return new R(false);
    }

    public static R err(String msg) {
        R r = new R(false);
        r.put("msg", msg);
        return r;
    }

    public static R err(int code, String msg) {
        R r = R.err(msg);
        r.put("code", code);
        return r;
    }

    public static R err(MyException e) {
        R r = R.err(e.getCode(), e.getMessage());
        return r;
    }


}
